<!--
 * @Descripttion: 
 * @version: 
 * @Author: lixx
 * @Date: 2025-07-08 16:37:36
 * @LastEditors: lixx
 * @LastEditTime: 2025-07-08 18:10:52
-->
<template>
  <el-card style="max-width: 480px">
    <el-form
      ref="ruleFormRef"
      style="max-width: 600px"
      :model="ruleForm"
      :rules="rules"
      label-width="auto"
    >
      <el-form-item label="账号 : " prop="user">
        <el-input v-model="ruleForm.user" />
      </el-form-item>
      <el-form-item label="密码 : " prop="password">
        <el-input v-model="ruleForm.password" />
      </el-form-item>
    </el-form>
    <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
  </el-card>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { useRouter } from 'vue-router'
import { login } from '../api/login'
const router = useRouter()
const ruleFormRef = ref<FormInstance>()

const ruleForm = ref({
  user: '',
  password: '',
})

const rules = ref({
  user: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      console.log('submit!')
      login(ruleForm.value).then((res) => {
        console.log(res, res.data.token)
        if (res.data.token) {
          console.log('登录成功')
          localStorage.setItem('token', res.data.token)
          router.push('/index')
        }
      })
    } else {
      console.log('error submit!')
      // 移除 return false，因为 FormValidateCallback 应返回 void 或 Promise<void>
    }
  })
}
</script>